<?php

namespace app\index\controller;

use app\index\model\Collect as collectModel;
use Qiniu\Auth;

vendor('qiniu/php-sdk/autoload.php');

use Qiniu\Storage\UploadManager;
use think\Db;

class My extends Base
{

    //我的订单
    public function my()
    {

        $param = request()->get();

        $type = 1;
        if (array_key_exists('type', $param)) {

            if ($param['type'] == 1) {
                $type = 1;
            } elseif ($param['type'] == 2) {
                $type = 2;
            } elseif ($param['type'] == 3) {
                $type = 3;
            } elseif ($param['type'] == 4) {
                $type = 4;
            }
        }

        $data = self::myOrder(session('uid'), $type);

        $this->assign('data', $data);
        return view();
    }

    private function myOrder($uid, $type)
    {
        if ($type == 1) {
            //全部
            $where = '';
        } else if ($type == 2) {
            //待支付
            $where['order_status'] = array('eq', 0);
        } else if ($type == 3) {
            //待出现
            $where['order_status'] = array('eq', 1);
        } else if ($type == 4) {
            //待评价
            $where['order_status'] = array('eq', 3);
        }
        $tour = Db::name('tour_order')
            ->alias('to')
            ->join('tours t', 'to.tour_id=t.tour_id', 'LEFT')
            ->join('tour_ticket tt', 'tt.tid=to.type_id', 'LEFT')
            ->field('to.order_id,to.order_sn,to.tour_id,to.ticket_date as use_time,to.order_status,to.pay_type,to.order_price,to.ticket_count,t.tour_name as store_name,to.create_time,tt.tname,tt.can_refund')
            ->where('to.user_id', $uid)
            ->where('to.is_del', 0)
            ->where($where)
            ->select();

        foreach ($tour as $k1 => $v1) {
            $tour[$k1]['kinds'] = 1;
        }

        $hotel = Db::name('hotel_order')->alias('ho')
            ->join('hotel h', 'ho.hotel_id=h.hotel_id', 'LEFT')
            ->join('hotel_room r', 'ho.room_id=r.room_id', 'left')
            ->field('ho.order_id,ho.order_sn,ho.hotel_id,ho.room_name,ho.pay_type,ho.room_amount,ho.create_time,ho.order_price,ho.order_status,ho.start_time as use_time,ho.end_time,h.hotel_name as store_name,r.is_quxiao')
            ->where('ho.uid', $uid)
            ->where('ho.is_del', 0)
            ->where($where)
            ->select();

        foreach ($hotel as $k2 => $v2) {

            $hotel[$k2]['kinds'] = 2;
        }

        $team = Db::name('gtour_order')->alias('go')
            ->join('group_tour gt', 'go.gtour_id=gt.id', 'LEFT')
            ->field('go.order_id,go.order_sn,go.gtour_id,go.gtour_number,go.pay_type,go.gtour_start as use_time,go.order_price,go.order_status,go.create_time,gt.gtour_title as store_name,gt.is_quxiao')
            ->where('go.user_id', $uid)
            ->where('go.is_del', 0)
            ->where($where)
            ->select();

        foreach ($team as $k3 => $v3) {

            $team[$k3]['kinds'] = 3;
        }

        $c = array_merge($tour, $hotel, $team);
        if (!empty($c)) {
            $last_names = array_column($c, 'create_time');
            array_multisort($last_names, SORT_DESC, $c);
        }
        return $c;
    }

    //我的收藏
    public function collect()
    {

        $collect = new collectModel();

        $tour = $collect->collects(session('uid'), 1, 1);
        $hotel = $collect->collects(session('uid'), 2, 1);
        $gentuan = $collect->collects(session('uid'), 3, 1);
        $food = $collect->collects(session('uid'), 4, 1);


        $gonglve = $collect->collects(session('uid'), 5, 1);


        $circle = $collect->collects(session('uid'), 6, 1);

        $this->assign('tour', $tour);
        $this->assign('hotel', $hotel);
        $this->assign('gentuan', $gentuan);
        $this->assign('food', $food);
        $this->assign('gonglve', $gonglve);
        $this->assign('circle', $circle);

        return view();
    }

    //我的消息
    public function message()
    {
        $uid = session('uid');
        $where['receive_user_id'] = $uid;
        $where['msg_type'] = 2;
        $orderMessageCount = Db::name('message')->where($where)->count();


        $where['msg_type'] = 1;
        $systemMessageCount = Db::name('message')->where($where)->count();

        $commentMessage = self::getCommentMessage();
        $this->assign('orderMessageCount',$orderMessageCount);
        $this->assign('systemMessageCount',$systemMessageCount);
        $this->assign('commentMessage',json_encode($commentMessage));

        return view();
    }

    //获取订单消息
    public function getOrderMessage(){
        if(request()->isAjax()){
            $post = request()->post();
            $uid = session('uid');

            $pagesize = 5;
            $page = (int)$post['page'];

            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }

            $where['receive_user_id'] = $uid;
            $where['msg_type'] = 2;
            $data = Db::name('message')
                ->where($where)
                ->order('mes_id','desc')
                ->limit($startrow, $pagesize)
                ->select();

            return json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
        }
    }

    //获取系统消息
    public function getSystemMessage(){
        if(request()->isAjax()){
            $post = request()->post();
            $uid = session('uid');

            $pagesize = 5;
            $page = (int)$post['page'];

            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }

            $where['receive_user_id'] = $uid;
            $where['msg_type'] = 1;
            $data = Db::name('message')
                ->where($where)
                ->order('mes_id','desc')
                ->limit($startrow, $pagesize)
                ->select();

            return json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
        }
    }

    private function getCommentMessage(){

            $post = array();
            $post['uid'] = session('uid');

            Db::name("comment_message")->where("uid",$post['uid'])->setField("is_read",1);
            Db::name("comment_method_message")->where('uid',$post['uid'])->setField('is_read',1);

            $where['t1.uid']  = $post['uid'];
            $data1 = Db::name('comment_method_message')->alias('t1')
                ->field("t1.type,t1.comment_content,t1.self_comment,t1.time,t4.nickname,t2.title,t2.image,t2.mid as mid,t4.user_icon")
                ->join('method t2','t1.wid = t2.mid','left')
                ->join('user t4','t1.comment_uid = t4.uid','left')
                ->where($where)
                ->order("t1.time desc")
                ->select();
            if(!empty($data1)){
                foreach($data1 as $k=>$v){
                    $data1[$k]['time'] = time_ago($v['time']);
                    $data1[$k]['video_covers'] = '';
                    $data1[$k]['status'] = 0;
                    $data1[$k]['is_yq'] = 1;
                }
            }

            $data2 = Db::name('comment_message')->alias('t1')
                ->field('t1.type,t1.comment_content,t1.self_comment,t4.nickname,
                t2.fc_title as title,t2.image,t2.f_id as mid,t4.user_icon,t2.video_covers,t2.status,t1.time')
                ->join('friends_circle t2','t1.wid = t2.f_id','left')
                ->join('user t4','t1.comment_uid = t4.uid','left')
                ->where($where)
                ->order("t1.time desc")
                ->select();
            if(!empty($data2)){
                foreach ($data2 as $k=>$v){
                    $data2[$k]['time'] = time_ago($v['time']);
                    $data2[$k]['is_yq'] = 2;
                }
            }
            $data = array_merge($data1,$data2);
            if(!empty($data)){
                foreach($data as $k=>$v){
                    $data[$k]['image'] = explode(',',$v['image'])[0];
                }
            }

            return $data;
    }

    public function other()
    {

        //获取用户信息
        $data = Db::name('user')->where('uid', session('uid'))->find();

        $data['hotel_store_status'] = 3; //3未入驻
        $data['food_store_status'] = 3;
        $data['team_store_status'] = 3;

        $user_id = session('uid');

        if ($data['status'] == 2) {

            $hotelStore = Db::name('hotel')->where('user_id', $user_id)->find();

            if (!empty($hotelStore)) {
                $data['hotel_store_status'] = $hotelStore['is_pass'];
            }
        } elseif ($data['status'] == 3) {

            $foodStore = Db::name('food_store')->where('uid', $user_id)->find();

            if (!empty($foodStore)) {
                $data['food_store_status'] = $foodStore['is_pass'];
            }
        } elseif ($data['status'] == 4) {

            $teamStore = Db::name('team')->where('uid', $user_id)->find();

            if (!empty($teamStore)) {
                $data['team_store_status'] = $teamStore['is_pass'];
            }
        }

        $this->assign('data', $data);
        return view();
    }

    /**
     * 景区封面图上传
     */
    public function uploadUserIcon()
    {
        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //后缀
        // 上传到七牛后保存的文件名
        $key = 'lv/user/' . substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = config("qiniu")["accessKey"];
        $secretKey = config("qiniu")["secretKey"];

        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = config("qiniu")["bucket"];
        //域名
        $domain = config("qiniu")["domain"];
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["code" => 0, "msg" => $err, "data" => ""];
        } else {
            //返回图片的完整URL
            $imgPath = $domain . '/' . $key;

            $edit = Db::name('user')->where('uid', session('uid'))->setField('user_icon', $imgPath);

            session('user_icon', $imgPath);

            return ["code" => 1, "msg" => "上传成功"];
        }
    }

    public function editNickName()
    {

        if (request()->isAjax()) {

            $param = request()->param();

            $res = Db::name('user')->where('uid', session('uid'))->setField('nickname', $param['nickname']);

            if ($res !== false) {

                session('nickname', $param['nickname']);
                return ['code' => 1];
            } else {

                return ['code' => 0];
            }
        }
    }

    public function editSex()
    {

        if (request()->isAjax()) {

            $param = request()->param();

            $res = Db::name('user')->where('uid', session('uid'))->setField('sex', $param['sex']);

            if ($res !== false) {

                return ['code' => 1];
            } else {

                return ['code' => 0];
            }
        }
    }

    public function applyHotel()
    {

        return view();
    }

    public function applyTour()
    {

        return view();
    }

    public function applyFood()
    {

        if (request()->isAjax()) {

            $param = request()->param();

            $addData = $param['data'];
            $addData['uid'] = session('uid');
            $addData['app_time'] = time();
            $addData['city'] = $param['data']['province'] . $param['data']['city'] . $param['data']['county'];
            $addData['fs_lagal'] = $addData['fs_lagal_1'] . ',' . $addData['fs_lagal_2'];
            unset($addData['fs_lagal_1']);
            unset($addData['fs_lagal_2']);
            unset($addData['province']);
            unset($addData['county']);

            unset($addData['file']);

            $getDoc = getDoc($addData['city'] . $addData['food_store_address']);

            $addData['food_store_longitude'] = $getDoc[0];
            $addData['food_store_latitude'] = $getDoc[1];

            $res = Db::name('food_store')->insert($addData);

            if ($res) {

                return ['code' => 1];
            } else {

                return ['code' => 0];
            }
        } else {
            return view();
        }
    }

    public function uploadApplyImg()
    {

        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //后缀
        // 上传到七牛后保存的文件名
        $key = 'lv/apply/' . substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = config("qiniu")["accessKey"];
        $secretKey = config("qiniu")["secretKey"];

        $auth = new Auth($accessKey, $secretKey);
        //要上传的空间
        $bucket = config("qiniu")["bucket"];
        //域名
        $domain = config("qiniu")["domain"];
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["code" => 0, "msg" => $err, "data" => ""];
        } else {
            //返回图片的完整URL
            $imgPath = $domain . '/' . $key;
            return ["code" => 1, "msg" => "上传成功", 'path' => $imgPath];
        }
    }

    public function publish()
    {
        //查询我的发布
        //功略
        $uid = session('uid');

        $methodWhere['t1.is_pass'] = 1;
        $methodWhere['t1.uid'] = $uid;
        $methodWhere['t1.is_del'] = 0;
        $methodWhere['t1.is_display'] = 0;

        $method = Db::name('method')
            ->alias('t1')
            ->field('
                    t1.*,
                    t2.nickname,t2.user_icon
                    ')
            ->join("lv_user t2", "t1.uid = t2.uid", 'left')
            ->where($methodWhere)
            ->order('is_select', 'desc')
            ->order('mid', 'desc')
            ->select();

        foreach ($method as $k => $v) {

            $method[$k]['id'] = $v['mid'];
            $method[$k]['title'] = $v['title'];

            $titleCount = self::utf8_strlen($v['title']);
            $method[$k]['title'] = $titleCount > 30 ? mb_substr($v['title'], 0, 30) . '...' : $v['title'];

            $image = explode(',', $v['image']);
            $method[$k]['image'] = $image[0];

            $content = json_decode($v['content'], true);
            $method[$k]['content'] = json_decode($v['content'], true);

            $content_str = '';
            foreach ($content as $k2 => $v2) {
                $content_str .= $v2['data'];
            }
            $method[$k]['content'] = mb_substr($content_str, 0, 45);
            $method[$k]['content'] .= '...';

            $method[$k]['like_num'] = $v['like_num'] * 1 + $v['set_like_num'] * 1;
            $method[$k]['read_num'] = $v['read_num'] * 1 + $v['set_read_num'] * 1;
            $method[$k]['share_num'] = $v['set_share_num'] * 1 + $v['share_num'] * 1;

            $method[$k]['is_video'] = 0;
            $method[$k]['is_method'] = 1;
            $method[$k]['nickname'] = $v['nickname'];
            $method[$k]['user_icon'] = $v['user_icon'];
            $method[$k]['time'] = time_ago($v['create_time']);
        }

        $circleTxtWhere['t1.is_pass'] = 1;
        $circleTxtWhere['t1.is_del'] = 0;
        $circleTxtWhere['t1.is_display'] = 0;
        $circleTxtWhere['t1.status'] = 1;
        $circleTxtWhere['t1.uid'] = $uid;

        $circleTx = Db::name('friends_circle')
            ->alias('t1')
            ->field('
                    t1.*,
                    t2.nickname,t2.user_icon
                    ')
            ->join("lv_user t2", "t1.uid = t2.uid", 'left')
            ->where($circleTxtWhere)
            ->order('f_id', 'desc')
            ->select();

        foreach ($circleTx as $k0 => $v0) {

            $circleTx[$k0]['id'] = $v0['f_id'];
            $titleCount = self::utf8_strlen($v0['fc_title']);
            $circleTx[$k0]['title'] = $titleCount > 30 ? mb_substr($v0['fc_title'], 0, 30) . '...' : $v0['fc_title'];

            if ($v0['status'] == 1) {

                $image = explode(',', $v0['image']);
                $circleTx[$k0]['image'] = $image[0];

                $circleTx[$k0]['content'] = mb_substr($v0['content'], 0, 45);
            } else {

                $circleTx[$k0]['image'] = $v0['video'] . "?vframe/jpg/offset/1";
                $circleTx[$k0]['content'] = mb_substr($v0['video_content'], 0, 45);
            }

            $circleTx[$k0]['like_num'] = $v0['like_num'] * 1 + $v0['set_like_num'] * 1;
            $circleTx[$k0]['read_num'] = $v0['read_num'] * 1 + $v0['set_read_num'] * 1;
            $circleTx[$k0]['share_num'] = $v0['set_share_num'] * 1 + $v0['share_num'] * 1;

            $circleTx[$k0]['is_video'] = $v0['status'] == 2 ? 1 : 0;
            $circleTx[$k0]['is_method'] = 0;

            $circleTx[$k0]['nickname'] = $v0['nickname'];
            $circleTx[$k0]['user_icon'] = $v0['user_icon'];
            $circleTx[$k0]['time'] = time_ago($v0['create_time']);
        }

        $circleVideoWhere['t1.is_pass'] = 1;
        $circleVideoWhere['t1.is_del'] = 0;
        $circleVideoWhere['t1.is_display'] = 0;
        $circleVideoWhere['t1.status'] = 2;
        $circleVideoWhere['t1.uid'] = $uid;

        $circleVideo = Db::name('friends_circle')
            ->alias('t1')
            ->field('
                    t1.*,
                    t2.nickname,t2.user_icon
                    ')
            ->join("lv_user t2", "t1.uid = t2.uid", 'left')
            ->where($circleVideoWhere)
            ->order('f_id', 'desc')
            ->select();

        foreach ($circleVideo as $k1 => $v1) {

            $circleVideo[$k1]['id'] = $v1['f_id'];
            $titleCount = self::utf8_strlen($v1['fc_title']);
            $circleVideo[$k1]['title'] = $titleCount > 30 ? mb_substr($v0['fc_title'], 0, 30) . '...' : $v1['fc_title'];

            if ($v1['status'] == 1) {

                $image = explode(',', $v1['image']);
                $circleVideo[$k1]['image'] = $image[0];

                $circleVideo[$k1]['content'] = mb_substr($v1['content'], 0, 45);
            } else {

                $circleVideo[$k1]['image'] = $v1['video'] . "?vframe/jpg/offset/1";
                $circleVideo[$k1]['content'] = mb_substr($v1['video_content'], 0, 45);
            }

            $circleVideo[$k1]['like_num'] = $v1['like_num'] * 1 + $v1['set_like_num'] * 1;
            $circleVideo[$k1]['read_num'] = $v1['read_num'] * 1 + $v1['set_read_num'] * 1;
            $circleVideo[$k1]['share_num'] = $v1['set_share_num'] * 1 + $v1['share_num'] * 1;

            $circleVideo[$k1]['is_video'] = $v1['status'] == 2 ? 1 : 0;
            $circleVideo[$k1]['is_method'] = 0;

            $circleVideo[$k1]['nickname'] = $v1['nickname'];
            $circleVideo[$k1]['user_icon'] = $v1['user_icon'];
            $circleVideo[$k1]['time'] = time_ago($v1['create_time']);
        }

        $this->assign('circleVideo', $circleVideo);
        $this->assign('circleTx', $circleTx);
        $this->assign('method', $method);
        return view();
    }

    public function foodController()
    {

        $user_id = session('uid');

        //查询该uid下的美食店铺

        $store = Db::name('food_store')->where('uid', $user_id)->find();
        if (!empty($store)) {

            $store['food_tag_ol'] = $store['food_tag'];
            $store['food_tag'] = explode(',', $store['food_tag']);

            //查询美食
            $food = Db::name('food')->field('food_title,food_cover')->where('store_id', $store['food_store_id'])->select();

            //查询收藏数
            $collectNum = Db::name('collect')->where('other_id', $store['food_store_id'])->where('type', 2)->count();

            $this->assign('collectNum', $collectNum);
            $this->assign('food', $food);
            $this->assign('store', $store);
            return view();
        } else {
            $this->error('非法操作', 'Index/index');
        }
    }

    public function uploadFoodCover()
    {

        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //后缀
        // 上传到七牛后保存的文件名
        $key = 'lv/foodcover/' . substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = config("qiniu")["accessKey"];
        $secretKey = config("qiniu")["secretKey"];

        $auth = new Auth($accessKey, $secretKey);
        //要上传的空间
        $bucket = config("qiniu")["bucket"];
        //域名
        $domain = config("qiniu")["domain"];
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["code" => 0, "msg" => $err, "data" => ""];
        } else {
            //返回图片的完整URL
            $imgPath = $domain . '/' . $key;
            return ["code" => 1, "msg" => "上传成功", 'path' => $imgPath];
        }
    }

    public function addFood()
    {

        $user_id = session('uid');

        //查询该uid下的美食店铺

        $store = Db::name('food_store')->where('uid', $user_id)->find();

        $param = request()->param();

        $addData = [
            'store_id' => $store['food_store_id'],
            'food_title' => $param['data']['food_title'],
            'food_cover' => $param['data']['food_cover'],
            'create_time' => date("Y-m-d H:i:s"),
            'update_time' => date("Y-m-d H:i:s")
        ];

        $res = Db::name('food')->insert($addData);

        if ($res) {
            return ['code' => 1];
        } else {
            return ['code' => 0];
        }
    }

    public function getFoodList()
    {

        $user_id = session('uid');

        //查询该uid下的美食店铺

        $store = Db::name('food_store')->where('uid', $user_id)->find();

        $dataList = Db::name('food')->field('food_title,food_cover')->where('store_id', $store['food_store_id'])->select();

        return ['code' => 1, 'data' => $dataList];
    }

    public function editStoreInfo()
    {

        $user_id = session('uid');

        //查询该uid下的美食店铺

        $store = Db::name('food_store')->where('uid', $user_id)->find();

        $param = request()->post();
        $editData = [

            'food_people_money' => $param['data']['food_people_money'],
            'food_store_time' => $param['data']['food_store_time'],
            'food_tag' => $param['data']['food_tag'],
            'food_store_content' => $param['data']['food_store_content']

        ];

        $res = Db::name('food_store')->where('food_store_id', $store['food_store_id'])->update($editData);

        if ($res !== false) {
            return ['code' => 1];
        } else {
            return ['code' => 0];
        }
    }

    public function getFoodStoreImgList()
    {

        $user_id = session('uid');

        //查询该uid下的美食店铺

        $store = Db::name('food_store')->where('uid', $user_id)->find();

        $data = Db::name('food_store_img')->where('store_id', $store['food_store_id'])->order('id', 'desc')->select();

        $count = Db::name('food_store_img')->where('store_id', $store['food_store_id'])->count();

        return ['code' => 1, 'data' => $data, 'count' => $count];
    }

    public function delStoreImage()
    {

        $param = request()->post();

        $res = Db::name('food_store_img')->where('id', $param['id'])->delete();

        if ($res) {
            return ['code' => 1];
        } else {
            return ['code' => 0];
        }
    }

    public function uploadStoreImg()
    {

        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //后缀
        // 上传到七牛后保存的文件名
        $key = 'lv/storeimg/' . substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = config("qiniu")["accessKey"];
        $secretKey = config("qiniu")["secretKey"];

        $auth = new Auth($accessKey, $secretKey);
        //要上传的空间
        $bucket = config("qiniu")["bucket"];
        //域名
        $domain = config("qiniu")["domain"];
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["code" => 0, "msg" => $err, "data" => ""];
        } else {
            //返回图片的完整URL
            $imgPath = $domain . '/' . $key;

            $user_id = session('uid');

            //查询该uid下的美食店铺

            $store = Db::name('food_store')->where('uid', $user_id)->find();

            $addData = [
                'store_id' => $store['food_store_id'],
                'img_path' => $imgPath,
                'create_time' => date('Y-m-d H:i:s')
            ];
            $addRes = Db::name('food_store_img')->insertGetId($addData);


            return ["code" => 1, "msg" => "上传成功", 'id' => $addRes, 'path' => $imgPath];
        }
    }

    private function utf8_strlen($string = null)
    {
        preg_match_all('/./us', $string, $match);
        return count($match[0]);
    }
}
